Conditions | 6 |
Total Lines | 147 |
Code Lines | 124 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import React from 'react'; |
||
10 | |||
11 | |||
12 | export default function Profile({navigation, setIsLoggedIn}): any { |
||
13 | const [balance, setBalance] = useState(null); |
||
14 | const [modalVisible, setModalVisible] = useState(false); |
||
15 | const [prepaid, setPrepaid] = useState(null); |
||
16 | |||
17 | const [firstname, setFirstname] = useState(null); |
||
18 | const [lastname, setLastname] = useState(null); |
||
19 | const [email, setEmail] = useState(null); |
||
20 | const [phonenumber, setPhonenumber] = useState(null); |
||
21 | |||
22 | //Get profile data for logged in user |
||
23 | useEffect(() => { |
||
24 | async function getUser(): Promise<void> { |
||
25 | const result = await userModel.getProfile(); |
||
26 | |||
27 | const profile = result['user']; |
||
28 | |||
29 | setFirstname(profile['firstName']); |
||
30 | setLastname(profile['lastName']); |
||
31 | setEmail(profile['email']); |
||
32 | setPhonenumber(profile['phoneNumber']); |
||
33 | |||
34 | }; |
||
35 | getUser(); |
||
36 | }, []); |
||
37 | |||
38 | function setFirstLastName(name: string): void { |
||
39 | const userName = name.split(' '); |
||
40 | |||
41 | const firstName = userName[0]; |
||
42 | const lastName = userName[1]; |
||
43 | |||
44 | setFirstname(firstName); |
||
45 | setLastname(lastName); |
||
46 | }; |
||
47 | |||
48 | async function logout(): Promise<void> { |
||
49 | await authModel.logout(); |
||
50 | setIsLoggedIn(false); |
||
51 | navigation.navigate('Auth'); |
||
52 | }; |
||
53 | |||
54 | async function save() { |
||
55 | // Prepare user data object |
||
56 | const userData = { |
||
57 | firstname: firstname, |
||
58 | lastname: lastname, |
||
59 | email: email, |
||
60 | phonenumber: phonenumber |
||
61 | }; |
||
62 | |||
63 | const result = await userModel.updateUser(userData); |
||
64 | |||
65 | if (Object.prototype.hasOwnProperty.call(result, 'errors')) { |
||
66 | showMessage({ |
||
67 | message: result['errors']['title'], |
||
68 | type: 'danger', |
||
69 | position: 'bottom' |
||
70 | }) |
||
71 | |||
72 | return; |
||
73 | }; |
||
74 | |||
75 | showMessage({ |
||
76 | message: result['message'], |
||
77 | type: 'success', |
||
78 | position: 'bottom' |
||
79 | }); |
||
80 | }; |
||
81 | |||
82 | return ( |
||
83 | <View style={styles.container}> |
||
84 | |||
85 | |||
86 | <View style={[styles.infoContainer]}> |
||
87 | |||
88 | <View style={styles.titleContainer}> |
||
89 | |||
90 | <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => navigation.navigate('Map')}> |
||
91 | <Icon |
||
92 | name='x' |
||
93 | size={25} |
||
94 | color='black' |
||
95 | /> |
||
96 | </Pressable> |
||
97 | |||
98 | <Text style={styles.title}>Profile</Text> |
||
99 | |||
100 | <Pressable style={styles.saveButton} onPress={() => save()}> |
||
101 | <Text style={styles.saveText}>Save</Text> |
||
102 | </Pressable> |
||
103 | |||
104 | </View> |
||
105 | |||
106 | |||
107 | |||
108 | |||
109 | <View style={styles.inputContainer}> |
||
110 | |||
111 | |||
112 | <TextInput |
||
113 | value={`${firstname} ${lastname}`} |
||
114 | style={styles.input} |
||
115 | onChangeText={(content: string) => { |
||
116 | setFirstLastName(content) |
||
117 | }} |
||
118 | |||
119 | /> |
||
120 | |||
121 | <TextInput |
||
122 | value={email} |
||
123 | style={styles.input} |
||
124 | keyboardType="email-address" |
||
125 | onChangeText={(content: string) => { |
||
126 | setEmail(content); |
||
127 | }} |
||
128 | /> |
||
129 | |||
130 | |||
131 | <TextInput |
||
132 | value={phonenumber} |
||
133 | style={styles.input} |
||
134 | keyboardType="phone-pad" |
||
135 | onChangeText={(content: string) => { |
||
136 | setPhonenumber(content); |
||
137 | }} |
||
138 | /> |
||
139 | </View> |
||
140 | |||
141 | <View style={styles.buttonContainer}> |
||
142 | <Pressable style={styles.prepaidButton} onPress={logout}> |
||
143 | <Text>Logout</Text> |
||
144 | </Pressable> |
||
145 | |||
146 | <Pressable style={[styles.prepaidButton, {backgroundColor: 'tomato'}]} onPress={() => { |
||
147 | setModalVisible(!modalVisible); |
||
148 | }}> |
||
149 | <Text>Delete account</Text> |
||
150 | </Pressable> |
||
151 | </View> |
||
152 | |||
153 | </View> |
||
154 | |||
155 | <DeleteModal navigation={navigation} modalVisible={modalVisible} setModalVisible={setModalVisible} setIsLoggedIn={setIsLoggedIn}></DeleteModal> |
||
156 | </View> |
||
157 | ) |
||
315 | }); |